﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework3_Winform
{
    public partial class Form1 : Form
    {
        // 在类级别定义file1Path和file2Path
        private string file1Path = string.Empty;
        private string file2Path = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // 保存选中文件的路径到file1Path
                file1Path = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                // 保存选中文件的路径到file2Path
                file2Path = openFileDialog2.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // 确保Data目录存在
            string dataDirectory = Path.Combine(Application.StartupPath, "Data");
            if (!Directory.Exists(dataDirectory))
            {
                Directory.CreateDirectory(dataDirectory);
            }

            // 设置新文件的路径
            string newFilePath = Path.Combine(dataDirectory, "MergedFile.txt");

            try
            {
                // 读取两个文件的内容并合并
                string content1 = File.ReadAllText(file1Path);
                string content2 = File.ReadAllText(file2Path);
                string mergedContent = content1 + Environment.NewLine + content2;

                // 写入新文件
                File.WriteAllText(newFilePath, mergedContent);

                MessageBox.Show("文件合并成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }
    }
}
